//
//  ViewController.m
//  05-Homework
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UIScrollView *smallScrollView;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatScrollView];
}

- (void)creatScrollView {
    NSArray *imageArr = @[@"0", @"1", @"2", @"3", @"4"];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(WIDTH * 7, 0);
    scrollView.contentOffset = CGPointMake(WIDTH, 0);
    [self.view addSubview:scrollView];
    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
    
    UIScrollView *firstScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [_scrollView addSubview:firstScrollView];
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImageView.image = [UIImage imageNamed:@"4"];
    [firstScrollView addSubview:firstImageView];
    
    UIScrollView *lastScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * (imageArr.count - 1), 0, WIDTH, HEIGHT)];
    [_scrollView addSubview:lastScrollView];
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    lastImageView.image = [UIImage imageNamed:@"0"];
    [lastScrollView addSubview:lastImageView];
    
    for (int i = 0; i < imageArr.count; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH + WIDTH * i, 0, WIDTH, HEIGHT)];
        smallScrollView.delegate = self;
        [_scrollView addSubview:smallScrollView];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:imageArr[i]];
        [smallScrollView addSubview:imageView];
        }
        
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH * 0.5, HEIGHT * 0.9, 50, 50)];
    self.page = page;
    page.numberOfPages = imageArr.count;
    [self.view addSubview:page];
    [page addTarget:self action:@selector(pageIndexChanged:) forControlEvents:UIControlEventValueChanged];
    
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    scrollView.zoomScale = 1.0;
}



- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    for (UIScrollView *sView in scrollView.subviews){
        
        if ([sView isKindOfClass:[UIScrollView class]]){
            sView.zoomScale = 1.0;
        } }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == WIDTH * 6) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    } else if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(WIDTH * 5, 0);
    }
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
}
- (void)pageIndexChanged:(UIPageControl *)page {
    self.scrollView.contentOffset = CGPointMake((page.currentPage + 1) * WIDTH, 0);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
